package pers.weiney.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import pers.weiney.pojo.Result;
import pers.weiney.utils.AliOssUtil;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author: @Weiney
 * @ClassName: FileUploadController
 * @Description: 文件上传控制器
 * @Create: 2024-08-14
 **/
@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        // 把文件内容上传到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        // 文件名设置为唯一的，防止文件覆盖
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        // file.transferTo(new File("D:\\BaiduNetdiskDownload\\file\\" + fileName));
        String fileUrl = AliOssUtil.uploadFile(fileName, file.getInputStream());
        return Result.success(fileUrl);
    }
}